package com.birdpiss.web.validators;

import java.math.BigDecimal;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;

import com.birdpiss.commons.utils.NumberUtils;
import com.birdpiss.web.domain.WebTransaction;

/**
 * The Class BirdpissValidator.
 *
 * @author mark
 */
@Service("birdpissValidator")
public class BirdpissValidator {

	private static final Logger log = Logger.getLogger(BirdpissValidator.class);
	private BigDecimal maxSingleTransactionAmount;

	/**
	 * Verify DI was setup correctly and perform any post DI initialization.
	 */
	@PostConstruct
	public void initialize() {
		if (this.maxSingleTransactionAmount == null) throw new BeanCreationException("Must set Max Transaction Amount");
	}

	/**
	 * Validate.
	 *
	 * @param webTransaction the web transaction
	 * @param errors the errors
	 */
	public void validate(WebTransaction webTransaction, Errors errors) {

		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "debitAccount.routingNumber", "emptyInputErrorMsg", new Object[] { "Routing Number" }, null);
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "debitAccount.accountNumber", "emptyInputErrorMsg", new Object[] { "Account Number" }, null);
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "amount", "emptyInputErrorMsg", new Object[] { "Amount" }, null);

		if (errors.hasErrors()) return;

		if (! StringUtils.hasText(webTransaction.getCreditUserId())) {
			if (log.isDebugEnabled()) log.debug("Credit User is invalid.");
			errors.reject("emptyInputErrorMsg", new Object[] { "Credit User" }, null);
			return;
		}

		if (! StringUtils.hasText(webTransaction.getAccountType())) {
			if (log.isDebugEnabled()) log.debug("Account Type is invalid.");
			errors.reject("emptyInputErrorMsg", new Object[] { "Account Type" }, null);
			return;
		}

		if ((webTransaction.getAmount() == null) || (webTransaction.getAmount().compareTo(BigDecimal.ZERO) != 1)) {
			if (log.isDebugEnabled()) log.debug("Amount must be greater than zero.");
			errors.reject("emptyInputErrorMsg", new Object[] { "Amount" }, null);
			return;
		}

		if (webTransaction.getAmount().compareTo(this.maxSingleTransactionAmount) == 1) {
			if (log.isDebugEnabled()) log.debug("Amount was over max single limit: " + this.maxSingleTransactionAmount);
			errors.reject("invalidMaxSingleAmount",
					      new Object[] { NumberUtils.setBigDecimalCurrencyScale(this.maxSingleTransactionAmount) },
					      "invalidMaxSingleAmount");
			return;
		}
	}

	/**
	 * Sets the max single transaction amount.
	 *
	 * @param maxSingleTransactionAmount the new max single transaction amount
	 */
	@Autowired
	public void setMaxSingleTransactionAmount(@Qualifier("maxSingleTransactionAmount") BigDecimal maxSingleTransactionAmount) {
		this.maxSingleTransactionAmount = maxSingleTransactionAmount;
	}

}
